package com.example.yqand4v1_1.controller;

import com.example.yqand4v1_1.beans.HttpResponseEntity;
import com.example.yqand4v1_1.dao.entity.Activity;
import com.example.yqand4v1_1.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @RequestMapping(value = "/queryActivitiesByAmount",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryActivitiesByAmount(@RequestParam("count")int count,@RequestParam("amount")int amount)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        List<Activity> activities=activityService.queryActivitiesByAmount(count,amount);
        if(CollectionUtils.isEmpty(activities))
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无活动");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(activities);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/participateActivity",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity participateActivity(@RequestBody Map<String,Object> map )
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        Activity activity = new Activity();
        int volunteerId =  Integer.parseInt(map.get("volunteerId").toString());
        Integer activityId = Integer.parseInt(map.get("activityId").toString());
        int peopleNum= Integer.parseInt(map.get("peopleNum").toString())+1;//报名成功了，人数要+1
        activity.setId(activityId);
        activity.setPeopleNum(peopleNum);
        int participateResult=activityService.participateActivity(volunteerId,activityId);
        int updateResult=activityService.updateActivity(activity);
        if(participateResult==0||updateResult==0)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("报名失败");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("已报名");
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/querySingleActivityById",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity querySingleActivityById(@RequestParam("activity_id")String activity_id)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int id=Integer.parseInt(activity_id);
        Activity activity=activityService.querySingleActivityById(id);
        if(activity==null)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无活动");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(activity);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/queryActivitiesIdOfVolunteer",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryActivitiesIdOfVolunteer(@RequestParam("volunteer_id")String volunteerId)
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int id=Integer.parseInt(volunteerId);
        ArrayList<Integer> activitiesId=activityService.queryActivitiesIdOfVolunteer(id);
        if(CollectionUtils.isEmpty(activitiesId))
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无活动");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(activitiesId);
        }
        return httpResponseEntity;
    }
    @RequestMapping(value = "/queryTotalNumOfActivities",method = RequestMethod.GET,headers = "Accept=application/json")
    public HttpResponseEntity queryTotalNumOfActivities()
    {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        int result=activityService.queryTotalNumOfActivities();
        if(result==0)
        {
            httpResponseEntity.setCode("204");
            httpResponseEntity.setMessage("无活动");
        }else {
            httpResponseEntity.setCode("200");
            httpResponseEntity.setMessage("成功查询");
            httpResponseEntity.setData(result);
        }
        return httpResponseEntity;
    }

}
